package com.doit.day02;

import com.alibaba.fastjson.JSON;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.sql.*;
import java.time.Duration;
import java.util.*;

/**
 * hashset
 * bitmap
 * redis bitmap hll
 * mysql
 */
public class Exercise_consumer3 {
    public static void main(String[] args) throws Exception {
        HashSet<Integer> set = new HashSet<>();
        //先搞出来一个consumer对象
        Properties props = new Properties();
        props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");
        props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.GROUP_ID_CONFIG,"linux01");
        //选配的
        props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"true");
        props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        //订阅主题
        consumer.subscribe(Arrays.asList("event"));
        //消费数据

        while (true){
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));
            for (ConsumerRecord<String, String> consumerRecord : poll) {
                //consumerRecord.value==> json  {"event":"cbshgIaYCP","guid":629946,"timestamp":1695801045439}
                String value = consumerRecord.value();
                // 解析json串
                EventLog eventLog = JSON.parseObject(value, EventLog.class);
                //判断这个用户是新用户还是老用户  ==》 怎么判断
                boolean flag = set.contains(eventLog.getGuid());
                if (flag){
                    eventLog.setIsNew(0);
                }else {
                    eventLog.setIsNew(1);
                    set.add(eventLog.getGuid());
                }
                System.out.println(JSON.toJSONString(eventLog));

            }
        }
    }
}

